package com.fox0g;

import com.fox0g.netty.NettyServerBootStrap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;

import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * @program: BookManage
 * @Date: 2022-07-10 11:16
 * @author: Fox0g
 * @description:
 */
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    NettyServerBootStrap serverBootStrap;

    public static void main(String[] args) {
        disableAccessWarnings(); // 忽略非法反射警告
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        serverBootStrap.start();
    }
    @Bean
    ServerEndpointExporter serverEndpointExporter(){
        return new ServerEndpointExporter();
    }

    /**
     * 忽略非法反射警告 适用于jdk11
     */
    @SuppressWarnings("unchecked")
    public static void disableAccessWarnings() {
        try {
            Class unsafeClass = Class.forName("sun.misc.Unsafe");
            Field field = unsafeClass.getDeclaredField("theUnsafe");
            field.setAccessible(true);
            Object unsafe = field.get(null);

            Method putObjectVolatile =
                    unsafeClass.getDeclaredMethod("putObjectVolatile", Object.class, long.class, Object.class);
            Method staticFieldOffset = unsafeClass.getDeclaredMethod("staticFieldOffset", Field.class);

            Class loggerClass = Class.forName("jdk.internal.module.IllegalAccessLogger");
            Field loggerField = loggerClass.getDeclaredField("logger");
            Long offset = (Long)staticFieldOffset.invoke(unsafe, loggerField);
            putObjectVolatile.invoke(unsafe, loggerClass, offset, null);
        } catch (Exception ignored) {
        }
    }



}
